package com.eshop.utils;

import java.util.Arrays;
import java.util.Locale;
import java.util.ArrayList;
import java.util.List;
import java.text.ParseException;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;

public class TimeUtils
{
    public static String setYearMonth(final String yearMonth) {
        try {
            final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            return sdf.format(sdf.parse(yearMonth));
        }
        catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
    
    public static int differentDaysByMillisecond(final String startTime, final String endTime) throws Exception {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        final Date date1 = sdf.parse(startTime);
        final Date date2 = sdf.parse(endTime);
        final int days = (int)((date2.getTime() - date1.getTime()) / 86400000L);
        return days;
    }
    
    public static boolean compareAfterTime(final String time) throws Exception {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.parse(time).getTime() >= new Date().getTime();
    }
    
    public static String today() {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date());
    }
    
    public static String yesterday() {
        final Calendar cal = Calendar.getInstance();
        cal.add(5, -1);
        final Date d = cal.getTime();
        final SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
        final String ZUOTIAN = sp.format(d);
        return ZUOTIAN;
    }
    
    public static String todayTime() {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(new Date());
    }
    
    public static String hhmm() {
        final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        return sdf.format(new Date());
    }
    
    public static String yyMMddHHmmss() {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }
    
    public static boolean isBelong(final String start, final String end) {
        final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = null;
        Date beginTime = null;
        Date endTime = null;
        try {
            now = df.parse(df.format(new Date()));
            beginTime = df.parse(start);
            endTime = df.parse(end);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        final boolean flag = belongCalendar(now, beginTime, endTime);
        return flag;
    }
    
    public static boolean belongCalendar(final Date nowTime, final Date beginTime, final Date endTime) {
        final Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        final Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);
        final Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        return date.after(begin) && date.before(end);
    }
    
    public static String getTomorrow(final int time) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(6, 1);
        cal.set(11, time);
        cal.set(12, 0);
        cal.set(13, 0);
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final String dateStr = sdf.format(cal.getTime());
        return dateStr;
    }
    
    public static String get24HourAfter() throws ParseException {
        final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(5, 1);
        final String date = format.format(calendar.getTime());
        return date;
    }
    
    public static String getMinuteAfter(final int time) throws ParseException {
        final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(12, time);
        final String date = format.format(calendar.getTime());
        return date;
    }
    
    public static String getMoreDayAfter(final String date, final int day) throws ParseException {
        final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final Calendar calendar = new GregorianCalendar();
        calendar.setTime(format.parse(date));
        calendar.add(5, day);
        return format.format(calendar.getTime());
    }
    
    public static String getMoreHourAfter(final String date, final int hour) throws ParseException {
        final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final Calendar calendar = new GregorianCalendar();
        calendar.setTime(format.parse(date));
        calendar.add(10, hour);
        return format.format(calendar.getTime());
    }
    
    public static String getMoreYearAfter(final String date, final int year) throws ParseException {
        final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        final Calendar calendar = new GregorianCalendar();
        calendar.setTime(format.parse(date));
        calendar.add(1, year);
        return format.format(calendar.getTime());
    }
    
    public static boolean compareBeforeToday(final String time) throws ParseException {
        final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.parse(time).getTime() <= new Date().getTime();
    }
    
    public static String getYearMonth() {
        final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        return format.format(new Date());
    }
    
    public static long getBetweenDays(final String startTime, final String endTime) {
        final Date date1 = parseDate(startTime, "yyyy-MM-dd");
        final Date date2 = parseDate(endTime, "yyyy-MM-dd");
        final Calendar calendar = Calendar.getInstance();
        calendar.setTime(date1);
        final long timeInMillis1 = calendar.getTimeInMillis();
        calendar.setTime(date2);
        final long timeInMillis2 = calendar.getTimeInMillis();
        final long betweenDays = (timeInMillis2 - timeInMillis1) / 86400000L;
        return betweenDays;
    }
    
    public static Date parseDate(final String dateStr, final String pattern) {
        final SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date;
        try {
            date = sdf.parse(dateStr);
        }
        catch (ParseException e) {
            throw new RuntimeException("日期转化错误");
        }
        return date;
    }
    
    public static String getMonthFirst() {
        Calendar cale = Calendar.getInstance();
        final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        cale = Calendar.getInstance();
        cale.add(2, 0);
        cale.set(5, 1);
        final String firstday = format.format(cale.getTime());
        return firstday;
    }
    
    public static String getMonthLast() {
        Calendar cale = Calendar.getInstance();
        final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        cale = Calendar.getInstance();
        cale.add(2, 1);
        cale.set(5, 0);
        final String lastday = format.format(cale.getTime());
        return lastday;
    }
    
    public static List<String> getDayByMonth(final int yearParam, final int monthParam) {
        final List list = new ArrayList();
        final Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        aCalendar.set(yearParam, monthParam, 1);
        final int year = aCalendar.get(1);
        final int month = aCalendar.get(2);
        for (int day = aCalendar.getActualMaximum(5), i = 1; i <= day; ++i) {
            String aDate = null;
            if (month < 10 && i < 10) {
                aDate = String.valueOf(year) + "-0" + month + "-0" + i;
            }
            if (month < 10 && i >= 10) {
                aDate = String.valueOf(year) + "-0" + month + "-" + i;
            }
            if (month >= 10 && i < 10) {
                aDate = String.valueOf(year) + "-" + month + "-0" + i;
            }
            if (month >= 10 && i >= 10) {
                aDate = String.valueOf(year) + "-" + month + "-" + i;
            }
            list.add(aDate);
        }
        return (List<String>)list;
    }
    
    public static Long getCountDownByTime(final String startTime, final String endTime) throws Exception {
        final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final Date start = format.parse(startTime);
        final Date end = format.parse(endTime);
        final Long time = end.getTime() - start.getTime();
        return time;
    }
    
    public static boolean compareTo(final String startTime, final String endTime) throws Exception {
        if (!EmptyUtils.isEmpty(startTime) && !EmptyUtils.isEmpty(endTime)) {
            final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            final Date bt = format.parse(startTime);
            final Date et = format.parse(endTime);
            return bt.after(et);
        }
        return false;
    }
    
    public static String rollYear(final String date, final int year) throws Exception {
        final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        final Date d = format.parse(date);
        final Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(1, year);
        return format.format(cal.getTime());
    }
    
    public static List<String> findDaysStr(final String begintTime, final String endTime) {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dBegin = null;
        Date dEnd = null;
        try {
            dBegin = sdf.parse(begintTime);
            dEnd = sdf.parse(endTime);
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        final List<String> daysStrList = new ArrayList<String>();
        daysStrList.add(sdf.format(dBegin));
        final Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(dBegin);
        final Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(dEnd);
        while (dEnd.after(calBegin.getTime())) {
            calBegin.add(5, 1);
            final String dayStr = sdf.format(calBegin.getTime());
            daysStrList.add(dayStr);
        }
        return daysStrList;
    }
    
    public static List<String> getBeforeSevenDay() {
        final String[] arr = new String[7];
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = null;
        for (int i = 0; i < 7; ++i) {
            c = Calendar.getInstance();
            c.add(5, -i - 1);
            arr[6 - i] = sdf.format(c.getTime());
        }
        return Arrays.asList(arr);
    }
    
    public static String headDate(final String date, final int hours) throws ParseException {
        final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final Calendar cal = Calendar.getInstance();
        cal.setTime(format.parse(date));
        cal.add(11, -hours);
        return format.format(cal.getTime());
    }
}
